package com.sina.interfaces.manage.web.controller.sys;

import com.sina.interfaces.core.exception.server.UpdateException;
import com.sina.interfaces.core.web.BaseController;
import com.sina.interfaces.domain.sys.Admin;
import com.sina.interfaces.manage.service.sys.AdminManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-11-03-16:18
 */
@Controller
@RequestMapping("adminManage")
public class AdminManageController extends BaseController {

    @Qualifier("adminManageService")
    @Autowired
    private AdminManageService adminManageService;

    //@RequiresRoles("admin")
    @RequestMapping(value = "getAdminList", method = RequestMethod.GET)
    public ModelAndView getAdminList(@Valid Admin admin) {
        LOG.info("admin:{}", admin);

        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("resultView");
        long count[] = new long[1];
        List<Map<String, Object>> adminList = adminManageService.getAdminList(admin, count);
        result.put("data", adminList);
        result.put("recordsFiltered", count[0]);
        mv.addObject(RESULT, result);

        return mv;
    }

    //@RequiresPermissions("select")
    @RequestMapping(value = "getAdmin", method = RequestMethod.GET)
    public ModelAndView getAdmin(String name) {
        LOG.info("name:{}", name);

        ModelAndView mv = new ModelAndView("resultView");
        Admin admin = adminManageService.getAdminByName(name);
        mv.addObject(RESULT, admin);

        return mv;
    }


    //@RequiresRoles("admin")
    @RequestMapping(value = "deleteAdmin", method = RequestMethod.GET)
    public ModelAndView deleteAdmin(String id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            adminManageService.deleteAdmin(Integer.valueOf(id));
            result.put(CODE, OK);
            result.put(MSG, "删除用户成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    //@RequiresRoles("admin")
    @RequestMapping(value = "updateAdmin", method = RequestMethod.GET)
    public ModelAndView updateAdmin(@Valid Admin admin) {
        LOG.info("admin:{}", admin);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            adminManageService.updateAdmin(admin);
            result.put(CODE, OK);
            result.put(MSG, "更新用户成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "saveAdmin", method = RequestMethod.GET)
    public ModelAndView saveAdmin(@Valid Admin admin) {
        LOG.info("admin:{}", admin);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            adminManageService.saveAdmin(admin);
            result.put(CODE, OK);
            result.put(MSG, "新增用户成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "getAdminListToHtml", method = RequestMethod.GET)
    public ModelAndView getAdminListToHtml() {

        ModelAndView mv = new ModelAndView("sys/adminList");
        return mv;
    }

}
